package com.uroad.jilinpda.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;


import com.uroad.jilinpda.R;
import com.uroad.jilinpda.modle.IssueMDL;
import com.uroad.jilinpda.widget.SYSDiaLogUtils;

import java.util.ArrayList;
import java.util.List;

public class IssueFailureAdapter extends RecyclerView.Adapter<IssueFailureAdapter.VH>{
    private List<IssueMDL.Data> mDatas = new ArrayList<>();

    private View mFooterView;
    public static final int TYPE_FOOTER = 1;
    public static final int TYPE_NORMAL = 2;
    private ItemRefund itemRefund;
    private Activity context;

    public IssueFailureAdapter(Activity context){
        this.context = context;
    }

    @Override
    public IssueFailureAdapter.VH onCreateViewHolder(ViewGroup parent, int viewType) {
        if(mFooterView != null && viewType == TYPE_FOOTER){
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_footer1, parent, false);
            return new IssueFailureAdapter.VH(v);
        }
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_issue_failure, parent, false);
        return new IssueFailureAdapter.VH(v);
    }

    public void setItemRefund(ItemRefund itemRefund){
        this.itemRefund = itemRefund;
    }

    @Override
    public void onBindViewHolder(IssueFailureAdapter.VH holder, @SuppressLint("RecyclerView") int position) {
        if(getItemViewType(position) == TYPE_NORMAL){
            holder.tv_plate_number.setText("车牌号："+mDatas.get(position).getNumberplate());
            holder.tv_plate_color.setText("车牌颜色："+mDatas.get(position).getName());
            holder.tv_time.setText(mDatas.get(position).getCompletetime());
        }else if (getItemViewType(position) == TYPE_FOOTER){
            holder.tv_footer.setText("加载更多数据。。。");
        }

        if (mDatas.get(position).getCheckstatus().equals("2")){
            holder.bt_refund.setVisibility(View.GONE);
            holder.bt_refund.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (itemRefund != null) {
                        itemRefund.OnRefund(mDatas.get(position).getOrderno(), mDatas.get(position).getHandleid());
                    }
                }
            });
        }
        if (!TextUtils.isEmpty(mDatas.get(position).getFailreason())){
            holder.tv_failure_cause.setVisibility(View.VISIBLE);
            holder.tv_failure_cause.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    SYSDiaLogUtils.showOnlyTextDialog(context,"失败原因",mDatas.get(position).getFailreason());
                }
            });
        }

    }


    public void setmDatas(List<IssueMDL.Data> mDatas){
        this.mDatas = mDatas;
        notifyDataSetChanged();
    }
    @Override
    public int getItemCount() {
        if(mFooterView != null){
            return mDatas.size() + 1;
        }
        return mDatas.size();
    }

    public void setFooterView(View footerView) {
        mFooterView = footerView;
        notifyItemInserted(getItemCount()-1);
    }

    public View getFooterView() {
        return mFooterView;
    }

    public void offFooterView(){
        mFooterView = null;
    }
    @Override
    public int getItemViewType(int position) {
        if (mFooterView == null){
            return TYPE_NORMAL;
        }
        if (position == getItemCount()-1){
            //最后一个,应该加载Footer
            return TYPE_FOOTER;
        }
        return TYPE_NORMAL;
    }

    public class VH extends RecyclerView.ViewHolder{
        public TextView tv_plate_number;
        public TextView tv_plate_color;
        private TextView tv_footer;
        private TextView tv_time;
        private Button bt_refund;
        private TextView tv_failure_cause;

        public VH(View v) {
            super(v);
            if (itemView == mFooterView){
                return;
            }
            tv_footer = (TextView) v.findViewById(R.id.tv_footer);
            tv_time = (TextView) v.findViewById(R.id.tv_time);
            tv_plate_number = (TextView) v.findViewById(R.id.tv_plate_number);
            tv_plate_color = (TextView) v.findViewById(R.id.tv_plate_color);
            bt_refund = (Button)v.findViewById(R.id.bt_refund);
            tv_failure_cause = (TextView) v.findViewById(R.id.tv_failure_cause);
        }
    }

    public interface ItemRefund {
        public void OnRefund(String orderno, String handleid);
    }
}
